﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;
using System.Globalization;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for FamilyService
    /// </summary>
    public class FamilyService : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var emp_id = "";//request.QueryString["empid"].ToString();
            var cmdemp = new Employee_Service(emp_id);
            var empObj = cmdemp.GetByEmpId();

            if (request["oper"] == "add")
            {
                var parent = DataSource.FirstOrDefault();
                if (parent != null)
                {
                    var child = parent.OTS_FAMILY;
                    var model = new OTS_FAMILY();
                    var rowix = child.Count;
                    if (parent.Action != ActionEnum.Create)
                    {
                        parent.Action = ActionEnum.Update;
                    } 
                    model.NAMEOFFATHER = request["NAMEOFFATHER"];
                    model.F_OCCUPATION = request["F_OCCUPATION"];
                    model.NAMEOFMOTHER = request["NAMEOFMOTHER"];
                    model.M_OCCUPATION = request["M_OCCUPATION"];
                    model.NUMOFBRO_SIS = Utility.CheckInt(request["NUMOFBRO_SIS"]);
                    model.RowIndex = rowix + 1;
                    model.Action = ActionEnum.Create;
                    child.Add(model);
                }
                else
                {
                    var p = new OTS_EMPLOYEE();
                    p.ID = "9.9999";
                    p.Action = ActionEnum.Create;
                    var child = new List<OTS_FAMILY>();
                    var model = new OTS_FAMILY();
                    var rowix = child.Count;
                    model.NAMEOFFATHER = request["NAMEOFFATHER"];
                    model.F_OCCUPATION = request["F_OCCUPATION"];
                    model.NAMEOFMOTHER = request["NAMEOFMOTHER"];
                    model.M_OCCUPATION = request["M_OCCUPATION"];
                    model.NUMOFBRO_SIS = Utility.CheckInt(request["NUMOFBRO_SIS"]);
                    model.RowIndex = rowix + 1;
                    child.Add(model);

                    p.OTS_FAMILY = child;
                    DataSource.Add(p);
                }
            }
            else
            {
                if (request["oper"] == "del")
                {
                    var parent = DataSource.FirstOrDefault();
                    if (parent != null)
                    {
                        var child = parent.OTS_FAMILY;
                        if (child != null)
                        {
                            var rowix = Convert.ToInt32(request["RowIndex"]);
                            var modeldel = child.Where(x => x.RowIndex == rowix).FirstOrDefault();
                            if (modeldel != null)
                            {
                                if (parent.Action == ActionEnum.Create)
                                {
                                    child.Remove(modeldel);

                                }
                                else
                                {
                                    parent.Action = ActionEnum.Update;
                                    modeldel.Action = ActionEnum.Delete;
                                }
                            }
                            else
                            {
                                rowix = Convert.ToInt32(request["id"]);
                                modeldel = child.Where(x => x.RowIndex == rowix).FirstOrDefault();
                                if (modeldel != null)
                                {
                                    if (parent.Action == ActionEnum.Create)
                                    {
                                        child.Remove(modeldel);

                                    }
                                    else
                                    {
                                        parent.Action = ActionEnum.Update;
                                        modeldel.Action = ActionEnum.Delete;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (request["oper"] == "edit")
                {
                    var parent = DataSource.FirstOrDefault();
                    if (parent != null)
                    {
                        var child = parent.OTS_FAMILY;
                        if (child != null)
                        {
                            var rowix = Convert.ToInt32(request["RowIndex"]);
                            var modeledit = child.Where(x => x.RowIndex == rowix).FirstOrDefault();
                            if (modeledit != null)
                            {
                                modeledit.NAMEOFFATHER = request["NAMEOFFATHER"];
                                modeledit.F_OCCUPATION = request["F_OCCUPATION"];
                                modeledit.NAMEOFMOTHER = request["NAMEOFMOTHER"];
                                modeledit.M_OCCUPATION = request["M_OCCUPATION"];
                                modeledit.NUMOFBRO_SIS = Utility.CheckInt(request["NUMOFBRO_SIS"]);
                                if (parent.Action == ActionEnum.Create)
                                {
                                    modeledit.Action = ActionEnum.Create;
                                }
                                else
                                {
                                    parent.Action = ActionEnum.Update;
                                    modeledit.Action = ActionEnum.Create;
                                }
                            }
                        }
                    }
                }
            }

            var list = new List<OTS_FAMILY>();
            var obj = DataSource.FirstOrDefault();
            if (obj != null)
            {
                var row = 0;
                var eduList = obj.OTS_FAMILY.ToList();
                foreach (var d in eduList)
                {
                    if (d.Action != ActionEnum.Delete)
                    {
                        var model = new OTS_FAMILY();
                        model.ID = d.ID;
                        model.NAMEOFFATHER = d.NAMEOFFATHER;
                        model.F_OCCUPATION = d.F_OCCUPATION;
                        model.NAMEOFMOTHER = d.NAMEOFMOTHER;
                        model.M_OCCUPATION = d.M_OCCUPATION;
                        model.NUMOFBRO_SIS = d.NUMOFBRO_SIS;
                        row = row + 1;
                        model.RowIndex = row;
                        list.Add(model);
                    }

                }
            }

            int total = list.Count == 0 ? 1 : (int)Math.Ceiling((double)list.Count / 10);

            var jsonData = new
            {
                total = total,
                page = request["page"],
                records = list.Count,
                rows = list.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
            };

            response.Write(new JavaScriptSerializer().Serialize(jsonData));
        }

        public List<OTS_EMPLOYEE> DataSource
        {
            get
            {
                var d = (List<OTS_EMPLOYEE>)HttpContext.Current.Session["emp"];
                if (d == null)
                {
                    d = new List<OTS_EMPLOYEE>();
                    HttpContext.Current.Session["emp"] = d;
                }
                return d;
            }
            set
            {
                HttpContext.Current.Session["emp"] = value;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}